package b2.widget.social
{
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class SocialIcon extends BasicIcon
	{
		public function SocialIcon()
		{
			var SocialIconBgr:Class = ResourceHelper.getInstance().getClassByName("SocialIcon");
			super(SocialIconBgr);
			this.visible = false;
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onMouseClick(event:Event):void
		{
			this.visible = false;
			ServerHelper.getInstance().getSocialInfo(false);
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_SOCIAL_INFO, onShowSocialMessageBox);
		}
		
		private function onShowSocialMessageBox(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_SOCIAL_INFO, onShowSocialMessageBox);
			var socialMessageBox:SocialMessageBox = new SocialMessageBox();
			socialMessageBox.x = 250;
			socialMessageBox.y = 180;
			WidgetManager.getInstance().popOut([socialMessageBox], true);
		}
		
		private function onAddedToStage(event:Event):void
		{
			ServerHelper.getInstance().checkNewSocialInfo(true);
			ServerHelper.getInstance().addEventListener(ServerEvent.CHECK_NEW_SOCIAL_INFO, onCheckNewSocialInfo);
		}

		private function onRemovedFromStage(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.CHECK_NEW_SOCIAL_INFO, onCheckNewSocialInfo);
		}
		
		private function onCheckNewSocialInfo(event:ServerEvent):void
		{
			visible = Boolean(event.userData);
			if (visible)
			{
				this.startShine();				
			}
			else
			{
				this.stopShine();
			}
		}
		
	}
}